package com.zx.controller;

import com.alibaba.fastjson.JSON;
import com.zx.entity.Order;
import com.zx.service.OrderService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-11
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    /**
     * 查询订单信息
     *
     * @param id 订单id
     * @return 订单
     */
    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public String findById(@PathVariable("id") Long id) {
        Order order = orderService.findById(id);
        return JSON.toJSONString(order);
    }


    /**
     * 添加订单
     *
     * @param order 订单实体
     * @return 订单
     */
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public String addOrder(@RequestBody Order order) {
        Order ord = orderService.addOrder(order);
        return JSON.toJSONString(ord);
    }

    /**
     * 修改订单
     *
     * @param order 订单实体
     * @return 订单
     */
    @PutMapping
    @ResponseStatus(HttpStatus.CREATED)
    public String updateProduct(@RequestBody Order order) {
        Order ord = orderService.updateOrder(order);
        return JSON.toJSONString(ord);
    }

    /**
     * 删除订单
     *
     * @param id 订单id
     */
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public String deleteProduct(@PathVariable("id") Long id) {
        orderService.deleteById(id);
        return "删除订单成功";
    }

}
